package com.zs.admin.service.wisdbook.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zs.admin.service.wisdbook.IWisdbookResourceService;
import com.zs.admin.validate.wisdbook.WisdbookResourceCreateValidate;
import com.zs.admin.validate.wisdbook.WisdbookResourceSearchValidate;
import com.zs.admin.validate.wisdbook.WisdbookResourceUpdateValidate;
import com.zs.admin.validate.commons.PageValidate;
import com.zs.admin.vo.wisdbook.WisdbookResourceDetailVo;
import com.zs.admin.vo.wisdbook.WisdbookResourceListedVo;
import com.zs.common.core.PageResult;
import com.zs.common.entity.wisdbook.WisdbookResource;
import com.zs.common.mapper.WisdbookResourceMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 资源实现类
 * @author SwAdmin
 */
@Service
public class WisdbookResourceServiceImpl implements IWisdbookResourceService {
        
    @Resource
    WisdbookResourceMapper wisdbookResourceMapper;



    /**
     * 资源列表
     *
     * @author SwAdmin
     * @param searchValidate 搜索参数
     * @return List<WisdbookResourceListedVo>
     */
    @Override
    public PageResult<WisdbookResourceListedVo> list(PageValidate pageValidate, WisdbookResourceSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<WisdbookResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        wisdbookResourceMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:linkType@link_type:int",
            "=:linkId@link_id:str",
        });

        IPage<WisdbookResource> iPage = wisdbookResourceMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<WisdbookResourceListedVo> list = new LinkedList<>();
        for(WisdbookResource item : iPage.getRecords()) {
            WisdbookResourceListedVo vo = new WisdbookResourceListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }


    @Override
    public int total(WisdbookResourceSearchValidate searchValidate) {
        QueryWrapper<WisdbookResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        wisdbookResourceMapper.setSearch(queryWrapper, searchValidate, new String[]{
                "=:linkType@link_type:int",
                "=:linkId@link_id:str",
        });
       long count= wisdbookResourceMapper.selectCount(queryWrapper);
       return Long.valueOf(count).intValue();
    }

    /**
     * 资源详情
     *
     * @author SwAdmin
     * @param id 主键参数
     * @return WisdbookResource
     */
    @Override
    public WisdbookResourceDetailVo detail(Integer id) {
        WisdbookResource model = wisdbookResourceMapper.selectOne(
                new QueryWrapper<WisdbookResource>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        WisdbookResourceDetailVo vo = new WisdbookResourceDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 资源新增
     *
     * @author SwAdmin
     * @param createValidate 参数
     */
    @Override
    public WisdbookResource add(WisdbookResourceCreateValidate createValidate) {
        WisdbookResource model = new WisdbookResource();
        model.setType(createValidate.getType());
        model.setTitle(createValidate.getTitle());
        model.setContent(createValidate.getContent());
        model.setLinkType(createValidate.getLinkType());
        model.setLinkId(createValidate.getLinkId());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        wisdbookResourceMapper.insert(model);

        return model;
    }






    /**
     * 资源编辑
     *
     * @author SwAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(WisdbookResourceUpdateValidate updateValidate) {
        WisdbookResource model = wisdbookResourceMapper.selectOne(
                new QueryWrapper<WisdbookResource>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");
        model.setId(updateValidate.getId());
        model.setType(updateValidate.getType());
        model.setTitle(updateValidate.getTitle());
        model.setContent(updateValidate.getContent());
        model.setLinkType(updateValidate.getLinkType());
        model.setLinkId(updateValidate.getLinkId());
        model.setUpdateTime(System.currentTimeMillis() / 1000);


        wisdbookResourceMapper.updateById(model);
    }

    /**
     * 资源删除
     *
     * @author SwAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        WisdbookResource model = wisdbookResourceMapper.selectOne(
                new QueryWrapper<WisdbookResource>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);



        wisdbookResourceMapper.updateById(model);



    }

}
